package com.ceit.controller;

import com.ceit.model.Administrators;
import com.ceit.model.Manager;
import com.ceit.model.User;
import com.ceit.service.AdministratorsS;
import com.ceit.util.JsonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 隼 on 2017/2/8.
 */

@Controller
@RequestMapping( value = "/Admin" )
public class administratorsControl {
    private void Output(String pass,String why,PrintWriter out){
        StringBuffer b=new StringBuffer();
        b.append("<R>");
        b.append("<pass>"+pass+"</pass>");
        b.append("<www>");
        b.append(why);
        b.append("</www>");
        b.append("</R>");
        System.out.println(b.toString());
        out.println(b.toString());
    }
    @Autowired
    private AdministratorsS ADMINS;
    JsonMapper nonNullBinder = JsonMapper.nonEmptyMapper();
    //添加超级管理页面
    @RequestMapping(value = "/AddAdministrator",method = RequestMethod.GET)
    public String AddAdministrator(){
        return  "Administrator/AddAdministrator";
    }

    //超级管理员列表
    @RequestMapping(value = "/ListAdministrator",method = RequestMethod.GET)
    public String ListAdministrator(Model model)throws Exception{
        List<Administrators> list=ADMINS.getAdminList();
        model.addAttribute("list",list);
        return  "Administrator/ListAdministrator";
    }

    //添加超级管理后台
    @RequestMapping(value = "/AddAdminS",method = RequestMethod.POST)
    public void AddAdS(HttpServletRequest request, HttpServletResponse response,Administrators administrators) throws Exception{
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String Ano=request.getParameter("Ano");
        int userid=-1;
        if(ADMINS.getByAno(Ano)!=null){
            StringBuffer b=new StringBuffer();
            Output("NO","Ano重复",out);
        }else if((ADMINS.getByNum(Ano))>0){
            Output("NO","账号已经存在，请换个Ano",out);
        }
        else{
            Boolean T=ADMINS.addUser(Ano);
            userid=ADMINS.getByNum(Ano);
            administrators.toString();
            administrators.setUserId(userid);
            Boolean TT=ADMINS.addADmin(administrators);
            Boolean TTT=ADMINS.addUserRole(userid,1);
            if(T==true&&TT==true&&TTT==true){
                Output("Yes","添加成功",out);
            }else {
                Output("NO","数据库出错",out);
            }
        }
    }

    @RequestMapping(value = "/UpdateAdministrator",method = RequestMethod.GET)
    public String UpdateAdministrator(Model model,HttpServletRequest request) throws Exception{
        Administrators administrators=ADMINS.getByAno(request.getParameter("AD"));
        if(administrators!=null) {
            model.addAttribute("ADmin",administrators);
        }
        return "Administrator/UpdateAdministrator";
    }


    @RequestMapping(value = "/UpdateAdministratorS",method = RequestMethod.POST)
    public void UpdateAdministratorS(HttpServletRequest request,HttpServletResponse response) throws Exception{
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String Ano=request.getParameter("Ano");
        String Aname=request.getParameter("Aname");
        String Asex=request.getParameter("Asex");
        String Aage=request.getParameter("Aage");
        String Adept=request.getParameter("Adept");
        String position=request.getParameter("position");
        Administrators administrators=ADMINS.getByAno(Ano);
        if(Ano!=null&Aname!=null&Asex!=null&Aage!=null&Adept!=null&position!=null){
            administrators.setAage((short) Integer.parseInt(Aage));
            administrators.setAdept(Adept);
            administrators.setAname(Aname);
            administrators.setAsex(Asex);
            administrators.setPosition(position);
            Boolean T= ADMINS.updateAD(administrators);
            if(T){
                Output("NO","参数错误",out);
            }else {
                Output("NO","数据库错误",out);
            }
        }else{
            Output("NO","参数错误",out);
        } 
    }



    @RequestMapping(value = "/updatepossword",method = RequestMethod.GET)
    public String updatepossword(HttpServletRequest request) throws Exception{
        return "Administrator/updatepossword";
    }


    @RequestMapping(value = "/updateposswordS",method = RequestMethod.POST)
    public void updateposswordS(HttpServletRequest request,HttpServletResponse response) throws Exception{
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        int  userid= (int) request.getSession().getAttribute("user");
        User user=ADMINS.getUserByuserid(userid);
       // System.out.println(user.getUserPassword());
        String password=request.getParameter("password");
        String newpassword=request.getParameter("newpassword");
        String repassword=request.getParameter("repassword");
        if(user==null||password==null||password==""||newpassword==null||newpassword==""||repassword==null||repassword==""){
            Output("NO","参数错误",out);
        }else if(newpassword.equals(repassword)&&user.getUserPassword().equals(password)){
            Boolean T=ADMINS.setPassword(newpassword,userid);
            if(T){
                Output("Yes","添加成功",out);
            }else {
                Output("NO","数据库错误",out);
            }
        }else {
            Output("NO","密码错误",out);
        }
    }



    @RequestMapping(value = "/addmanager",method = RequestMethod.GET)
    public String AddManager() throws Exception{
        return "Administrator/AddManager";
    }


    @RequestMapping(value = "/addmanagerS",method = RequestMethod.POST)
    public  void addmanagerS(HttpServletRequest request, HttpServletResponse response, Manager manager) throws Exception{
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String Mno=request.getParameter("Mno");
        System.out.println(manager.toString());
        if(ADMINS.getByMno(Mno)!=null){
            StringBuffer b=new StringBuffer();
            Output("NO","Mno重复",out);
        }else if((ADMINS.getByNum(Mno))>0){
            Output("NO","账号已经存在，请换个Mno",out);
        }
        else{
            int userid=-1;
            Boolean T=ADMINS.addUser(Mno);
            userid=ADMINS.getByNum(Mno);
            manager.setUserId(userid);
            Boolean TT =ADMINS.addManage(manager);
            Boolean TTT=ADMINS.addUserRole(userid,2);
            if(T==true&&TT==true&&TTT==true){
                Output("Yes","添加成功",out);
            }else {
                Output("NO","数据库出错",out);
            }
        }


    }

}
